//index.js
//获取应用实例
var app = getApp();
var goodslist = [];
var goods_spec = {};
Page({
  data: {
    shop_list_select: [],
    shoppingCartList: [],
    goods: [],
    tabid: 0,
    popup: 0,
    popup2: 0,
    data: {},
    choose_property_data: {},
    choose_spec: {},
    disabled: 0,
    spec_keys_str: '',
    config: {},
  },
  btn_cancel: function () {
    this.setData({ popup: 0 });
  },
  show_popup: function () {
    this.setData({ popup: 1 });
  },
  bindChange: function (e) {
    var value = e.detail.value;
    var shop_info = this.data.shop_list[value];
    this.setData({ shop_info: shop_info });
    wx.getStorageSync("shop_info", shop_info);
    this.getPageConfig();
  },
  onLoad: function (options) {
   
    var extConfigSync = app.getExtConfigSync();
    var that = this;
    var shop_info = app.getShopInfo();
    var shop_list_select = [];
    for (var i in extConfigSync.shop_list) {
      shop_list_select.push(extConfigSync.shop_list[i].name);
    }
    this.setData({ theme: extConfigSync.theme, shop_info: shop_info})
    
    this.getPageConfig();
  },
  //页面配置
  getPageConfig: function () {
    app.api("mall/pages/getPageConfig", {
      page: "pages/template/tags",
      shop_id: this.data.shop_info.id
    }, (res) => {
      if (res.code != 200) {
        app.alert(res.msg);
      }
      this.setData({ config: res.data });
      this.loadList();
    })
  },
  //商品列表
  loadList: function () {
    var that = this;

    var config = this.data.config;
    if (!config.attrs) {
      that.setData({
        goods: []
      });
      return;
    }

    this.setData({ config: config });

    for (let i in config.attrs) {
      var params = config.attrs[i].params;
      app.api("mall/goods/get", params, (res) => {
        if (res.code != 200) {
          app.alert(res.msg);
        }

        if (res.data && res.data.list) {
          goodslist[i] = res.data.list;
        } else {
          goodslist[i] = [];
        }
        var goods = []
        for (var k in goodslist[i]) {
          var item = goodslist[i][k];
          goods.push({
            id: item['id'],
            default_img: item['default_img'],
            goods_name: item['goods_name'],
            origin_price: item['origin_price'],
            price: item['price'],
            total_stock: item['total_stock'],
            sub_title: item['sub_title'],
          })
        }
        that.data.goods[i] = goods;
        that.setData({
          goods: that.data.goods
        });

      })
    }
  },
  showTabs: function (e) {
    wx.showLoading();
    var id = e.currentTarget.dataset.id;
    if (this.data.tabid == id) {
      return;
    }
    this.setData({ tabid: id });
    wx.hideLoading();
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getPageConfig();
    wx.stopPullDownRefresh();
  },

})
